#include <stdlib.h>

#define Elem char*
#define ALLOC_FAIL() do{printf("malloc failed");exit(-1);}while(0)

typedef struct{
	Elem * arr;
	int top;
	int cap;
}stack,*stackptr;

stackptr new_stack(int cap){
	stackptr st = (stackptr)malloc(sizeof(stack));
	if (!st) ALLOC_FAIL();
	st->arr = (Elem *)malloc(sizeof(Elem) * cap);
	if (!st->arr) ALLOC_FAIL();
	st->cap = cap;
	st->top = 0;
	return st;
}

void destruct_stack(stackptr st){
	if (!st) return;
	free(st->arr);
	free(st);
}

int pop(stackptr st, Elem * elem){
	if (st->top == st->cap){
		printf("Stack is full");
		return false;
	}
	*elem = st->arr[--st->top];
	return true;
}

int push(stackptr st, Elem elem){
	if (0 == st->top){
		printf("Stack is empty");
		return false;
	}
	st->arr[st->top++] = elem;
	return true;
}

int peer(stackptr st, Elem * elem){
	if (st->top == st->cap){
		printf("Stack is full");
		return false;
	}
	*elem = st->arr[st->top];
	return true;
}

int travel_stack(stackptr st,int (*visit)(Elem elem)){
	int flag = 0;
	for (int i = 0; i < top; ++i){
		if (visit(st->visit[i]))
			flag = 1;
		else{
			flag = 0;break;
		}
	return flag;
	}
